package com.neo.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.neo.constant.MessageConstant;
import com.neo.constant.RedisConstant;
import com.neo.entity.PageResult;
import com.neo.entity.QueryPageBean;
import com.neo.entity.Result;
import com.neo.pojo.Setmeal;
import com.neo.service.SetmealService;
import com.neo.utils.QiniuUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import javax.ws.rs.GET;
import java.util.List;
import java.util.UUID;

/**
 * 体检套餐
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {


    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return setmealService.pageQuery(queryPageBean);
    }


    @PostMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        String fileName = generateFileName(imgFile.getOriginalFilename());
        try {
            //将文件上传到七牛云服务器
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
            //讲上传图片的文件名存储到set集合
            if(StringUtils.isNotBlank(fileName)){
                jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
            }
            return Result.ok(MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
        } catch (Exception e) {
            return Result.fail(MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    private String generateFileName(String originalFilename) {
        String prefix = UUID.randomUUID().toString().replace("-", "");
        String extName = StringUtils.substringAfterLast(originalFilename, ".");
        return prefix + "." + extName;
    }


    /**
     * 套餐添加
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.add(setmeal,checkgroupIds);
            //添加成功后将存储到数据库的图片名保存到redis
            if(StringUtils.isNotBlank(setmeal.getImg())){
                jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
            }
            return Result.ok(MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            return Result.fail(MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @GetMapping("/findCheckGroupIdsBySetmeal")
    public Result findCheckGroupIdsBySetmeal(Integer id){
        try {
            List<Integer> checkgroupIds =  setmealService.findCheckGroupIdsBySetmealId(id);
            return Result.ok(MessageConstant.QUERY_SETMEAL_SUCCESS, checkgroupIds);
        } catch (Exception e){
            return Result.fail(MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        try {
            Setmeal setmeal = setmealService.findById(id);
            return Result.ok(MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
        } catch (Exception e){
            return Result.fail(MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        try {
            setmealService.edit(setmeal, checkgroupIds);

            //编辑成功后将存储到数据库的图片名保存到redis
            if(StringUtils.isNotBlank(setmeal.getImg())){
                jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
            }

            return Result.ok(MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e){
            return Result.fail(MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }

    @GetMapping("/delete")
    public Result deleteById(Integer id) {
        try {
            //七牛云图片删除
            String imageName = setmealService.delete(id);
            QiniuUtils.deleteFileFromQiniu(imageName);

            //redis移除数据库已删除的图片
            if(StringUtils.isNotBlank(imageName)){
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES, imageName);
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES, imageName);
            }

            return Result.ok(MessageConstant.DELETE_SETMEAL_SUCCESS);
        } catch (Exception e) {
            return Result.fail(MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }
}
